from DrissionPage import ChromiumPage
import time
import pandas as pd


def get_data():
    url = "https://www.maomaozu.com/#/build"
    page = ChromiumPage()
    page.get(url)
    res = []
    for i in range(3):
        print(f"正在采集第{i+1}页数据")
        print("-" * 30)
        data_ls = page.eles('x:/html/body/div/div[2]/div[2]/div/div[1]/div[1]/block')
        for data in data_ls:
            name = data.ele('x:./a/div[2]/div/span[@class="pc_of_all_p4 "]/text()')
            price = "￥" + data.ele('x:/a/div[3]/div/span[2]/text()') + "/m²·天"
            loc_data = data.ele('x:./a/div[2]/div[2]/span[2]')
            loc = loc_data.ele('x:./text()') + ''.join(loc_data.eles('x:./span/text()'))
            sub = ' '.join(data.eles('x:./a/div[2]/div[3]/span[@class="pc_of_all_psss1"]/text()'))
            date = data.ele('x:./a/div[2]/div[4]/span[2]/text()')
            area = ' '.join(data.eles('x:./a/div[2]/div[5]/a/span/text()'))
            dic = {
                "name": name,
                "price": price,
                "loc": loc,
                "sub": sub,
                "date": date,
                "area": area
            }
            print(dic)
            res.append(dic)
        page.scroll.to_bottom()
        btn = page.ele('x://button[@class="btn-next"]')
        btn.click()
        time.sleep(1)
    save(res)


def save(res):
    df = pd.DataFrame(res)
    df.to_excel("maomaozu.xlsx", index=False)


if __name__ == '__main__':
    get_data()
